package com.qiaoba.module.system.converter;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiaoba.api.system.entity.dto.SysRoleDTO;
import com.qiaoba.api.system.entity.vo.SysRoleVO;
import com.qiaoba.module.system.entity.SysRole;
import com.qiaoba.module.system.entity.dto.DataScopeDTO;
import com.qiaoba.module.system.entity.param.SysRoleParam;

import java.util.Objects;

/**
 * 角色 类型转换
 *
 * @author ailanyin
 * @date 2023-09-10 14:13:43
 */
public class SysRoleConverter {

    public static SysRole dto2Entity(SysRoleDTO dto) {
        SysRole sysRole = new SysRole();
        sysRole.setRoleId(dto.getRoleId());
        sysRole.setRoleName(dto.getRoleName());
        sysRole.setRoleKey(dto.getRoleKey());
        sysRole.setRoleSort(dto.getRoleSort());
        sysRole.setDataScope(dto.getDataScope());
        sysRole.setMenuCheckStrictly(dto.getMenuCheckStrictly());
        sysRole.setDeptCheckStrictly(dto.getDeptCheckStrictly());
        sysRole.setStatus(dto.getStatus());
        sysRole.setRemark(dto.getRemark());
        return sysRole;
    }

    public static SysRole dto2Entity(DataScopeDTO dto) {
        SysRole sysRole = new SysRole();
        sysRole.setRoleId(dto.getRoleId());
        sysRole.setDataScope(dto.getDataScope());
        return sysRole;
    }

    public static SysRoleVO domain2Vo(SysRole sysRole, boolean isHas) {
        SysRoleVO sysRoleVO = new SysRoleVO();
        sysRoleVO.setRoleId(sysRole.getRoleId());
        sysRoleVO.setRoleName(sysRole.getRoleName());
        sysRoleVO.setRoleKey(sysRole.getRoleKey());
        sysRoleVO.setRoleSort(sysRole.getRoleSort());
        sysRoleVO.setDataScope(sysRole.getDataScope());
        sysRoleVO.setStatus(sysRole.getStatus());
        sysRoleVO.setFlag(isHas);
        return sysRoleVO;
    }

    public static QueryWrapper<SysRole> param2Wrapper(SysRoleParam param) {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByAsc(SysRole::getRoleSort)
                .eq(StrUtil.isNotBlank(param.getStatus()), SysRole::getStatus, param.getStatus())
                .ge(Objects.nonNull(param.getBeginTime()), SysRole::getCreateTime, param.getBeginTime())
                .le(Objects.nonNull(param.getEndTime()), SysRole::getCreateTime, param.getEndTime())
                .like(StrUtil.isNotBlank(param.getRoleName()), SysRole::getRoleName, param.getRoleName())
                .like(StrUtil.isNotBlank(param.getRoleKey()), SysRole::getRoleKey, param.getRoleKey());
        return wrapper;
    }
}
